import { createRouter, createWebHistory } from 'vue-router';
import LayoutPage from '../components/LayoutPage.vue';
import LoginPage from '../views/LoginPage.vue';
import DashboardPage from '../views/DashboardPage.vue';
import OrderManagement from '../views/OrderManagement.vue';
import MerchantManagement from '../views/MerchantManagement.vue';
import UserManagement from '../views/UserManagement.vue';
import AdminSettings from '../views/AdminSettings.vue';
import AdminManagement from '@/views/AdminManagement.vue';
import DeviceManagement from '../views/DeviceManagement.vue';
import SelfServicePointManagement from '@/views/SelfServicePointManagement.vue';
import EarningsManagement from '../views/EarningsManagement.vue';
import SelfServiceOrderManagement from '../views/SelfServiceOrderManagement.vue';
import MerchantOrderManagement from '@/views/MerchantOrderManagement.vue';
import LaundryOptionManagement from '@/views/LaundryOptionManagement.vue';
import AdminRentalManagement from '@/views/AdminRentalManagement.vue';
import CarouselManagement from '@/views/CarouselManagement.vue';
import LaundryTipManagement from '@/views/LaundryTipManagement.vue';
import ForumManagement from '@/views/ForumManagement.vue';

const routes = [
    { path: '/', component: LoginPage, meta: { noCache: true } },
    {
        path: '/admin',
        component: LayoutPage,
        children: [
            { path: '', redirect: 'dashboard' },
            { path: 'dashboard', component: DashboardPage, meta: { noCache: true } },
            { path: 'admin-management', component: AdminManagement, meta: { noCache: true } },
            { path: 'order-management', component: OrderManagement, meta: { noCache: true } },
            { path: 'earnings-management', component: EarningsManagement, meta: { noCache: true } },
            { path: 'merchant-management', component: MerchantManagement, meta: { noCache: true } },
            { path: 'user-management', component: UserManagement, meta: { noCache: true } }, // 更新为新用户管理界面
            { path: 'self-service', component: SelfServicePointManagement, meta: { noCache: true } },
            { path: 'order-management/self-service', component: SelfServiceOrderManagement, meta: { noCache: true } },
            { path: 'order-management/merchant', component: MerchantOrderManagement, meta: { noCache: true } },
            { path: 'device-management', component: DeviceManagement, meta: { noCache: true } },
            { path: 'admin-settings', component: AdminSettings, meta: { noCache: true } },
            { path: 'laundry-option-management', component: LaundryOptionManagement, meta: { noCache: true } },
            { path: 'rental-management', component: AdminRentalManagement, meta: { noCache: true } },
            { path: 'carousel-management', component: CarouselManagement, meta: { noCache: true } },
            { path: 'laundry-tip-management', component: LaundryTipManagement, meta: { noCache: true } },
            { path: 'forum-management', component: ForumManagement, meta: { noCache: true } },
        ],
    },
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    const token = sessionStorage.getItem('token');
    if (token && to.path === '/') {
        next('/admin/dashboard');
    } else if (!token && to.path.startsWith('/admin')) {
        next('/');
    } else {
        next();
    }
});

export default router;